package com.fengba.nanzhi.setup;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.NumberUtil;

import java.text.ParseException;

/**
 * Created by SpakeBrony on 2018/4/9.
 */

public class ForPhone extends AppCompatActivity {

    //身份证号码
    private EditText FPnumber;


    //密码
    private EditText FPpassword;

    public ForPhone() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_forphone);


        FPnumber = (EditText) findViewById(R.id.FPnumber);
        FPpassword = (EditText) findViewById(R.id.FPpassword);


        /**
         * 返回
         */
        ImageButton FPbtn02 = (ImageButton) findViewById(R.id.FPbtn02);
        FPbtn02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        /**
         * 下一步
         */
        Button FPbtn01 = (Button) findViewById(R.id.FPbtn01);
        FPbtn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String number= null;
                try {
                    number = NumberUtil.IDCardValidate(String.valueOf(FPnumber.getText()));

                if (number.equals("")) {
                    Intent intent = new Intent(ForPhone.this, ForPhone02.class);
                    startActivity(intent);
                }else
                    {
                        Toast.makeText(ForPhone.this, number, Toast.LENGTH_SHORT).show();
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });

    }//都在这里打


    /**
     * 隐藏软键盘
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }
    public  boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }


}